package com.ineatconseil.yougo.client.ui.main.application.tabs.userTypesMngt;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.ineatconseil.yougo.client.cto.UserTypeCTO;
import com.ineatconseil.yougo.client.i18n.YougoLabelConstants;
import com.ineatconseil.yougo.client.ui.common.ClientFactory;

/**
 * @author Benjamin Barbier - Ineat Conseil
 */
public class UserTypesActivity extends AbstractActivity implements IUserTypesView.Presenter {

	private final ClientFactory clientFactory;
	private IUserTypesView userTypesView;
	private YougoLabelConstants constants;
	
	public UserTypesActivity(UserTypesPlace place, ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
		userTypesView = this.clientFactory.getUserTypesView();
		//TODO: do something with the place
	}
	
	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		System.out.println("UserTypesActivity - start()");
		constants = clientFactory.getLabelConstants();
		fillUserTypesTable();
	}
	
	/**
	 * 
	 */
	private void fillUserTypesTable() {
		clientFactory.getUserClientService().getUserTypes(
				new AsyncCallback<JsArray<UserTypeCTO>>() {
			@Override
			public void onFailure(Throwable caught) {
			}
			@Override
			public void onSuccess(JsArray<UserTypeCTO> result) {
				userTypesView.fillTable(Integer.parseInt(constants.pageSize()), result);
			}
		});
	}

	@Override
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);
	}
	
}
